<?php

require('config.php');

if(!isset($_SESSION['id_user']))
    redirectFront('connexion');

$_SESSION['new_message'] = modelUser::hasNewMessage($_SESSION['id_user']);

	$profilByUrl = modelProvince::getProvinceByUrl('profil.php');
	if($profilByUrl){
		$balise_title = $profilByUrl->title;
		$description = $profilByUrl->meta_title;
		$id_menu_left_tree = modelProvince::getProvinceMenuleft($profilByUrl->language_link)->id_menu_left_tree;
	}else{
		$balise_title = 'profil_balise';
		$description = 'profil_description';
	}
$page = new rootLayoutPage();
$page->setTranslatePage(__FILE__);
$page->setBaliseTitle($balise_title);
$page->setDescription($description);
//$page->addJavaScript('profil-upload-photo.js');
//$page->addJavaScript('ajaxUpload.js');

//-- menu Left show products--------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menuPage->setTable('profil');
$menuPage->setIdShow($id_menu_left_tree);
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

$header = new layoutHeader();
$header->setIdShow($id_menu_left_tree);
$page->setHeader($header);
//-- corps de la page --------//
$tpl_file='profil.tpl';
$profil = new flyLayout(REP_TPL . $tpl_file);
$profil->start();
$language_folder = $_SESSION['language_code']==language_default?'':'/'.$_SESSION['language_code'];
$profil->setVariable('language_folder', $language_folder);
//$tabs = new layoutTabs();
//$tabs->pushLink('Mon compte', '/profil.php');
//$tabs->pushLink('Mes messages', '/profil-message.php');
//$profil->includeLayout('tabs', $tabs);

/*$menu = new layoutMenu();
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);*/

$user = modelUser::getProfil($_SESSION['id_user']);
$login = modelUser::getIdUser($_SESSION['id_user']);
$profil->setVariable('address_email', $login->login);
	if($user){
	$profil->setVariable('nom', $user->nom);
	$profil->setVariable('prenom', $user->prenom);
	$profil->setVariable('city', $user->city);

	$lan = "nom_pays_".$_SESSION['language_code'];
	$country = modelUser::getListCountryById($user->id_country);
	$profil->setVariable('country', $country->$lan?$country->$lan:'<i>inconnue</i>');
	
	$profil->setVariable('i_am', $user->i_am?$user->i_am:'<i>inconnue</i>');	
	
	if($user->agency_name){
		$profil->setVariable('agency', $user->agency_name);
		$profil->showBlock('detail_agency');
	}
	if($user->website){
		$profil->setVariable('website', $user->website);
		$profil->showBlock('detail_website');
	}
	$language_folder = $_SESSION['language_code']==language_default?'':$_SESSION['language_code'].'/';
	$list_content_page = modelUser::getPageByIdUser($user->id_user);
	if($list_content_page->size()>0){
		foreach($list_content_page as $key=>$content_page){
			$profil->setVariable('title_page', $content_page->title);
			$profil->setVariable('url_page', HTTP_HOST.$language_folder.$content_page ->url_rewriting.'-'.$content_page->language_link.'.html');			
			$profil->parseList('detail_page');
		}
		$profil->showBlock('detail_page');
	}
	
	$ti = "titre_court_".$_SESSION['language_code'];
	$titre = modelUser::getTitreById($user->id_titre);
	$profil->setVariable('titre', $titre->$ti?$titre->$ti:'<i>inconnue</i>');

	$profil->setVariable('email_check', $user->public_email?'checked="checked"':'');
	$profil->setVariable('name_check', $user->public_name?'checked="checked"':'');
	$profil->setVariable('city_check', $user->public_city?'checked="checked"':'');
	$profil->setVariable('country_check', $user->public_country?'checked="checked"':'');
	$listPhone = modelUser::getListUserPhoneNumber($user->id);
	foreach($listPhone as $kkk => $phone){
		$profil->setVariable('phone_check', $phone->public?'checked="checked"':'');
		$profil->setVariable('phone', $phone->phone_number);
		$listSpeaker = modelUser::getListLanguageSpeakerByID($phone->id_phone_number_language);
		$s='';
		foreach($listSpeaker as $ss => $speaker){
			$phone_name = 'language_'.$_SESSION['language_code'];
			$listSpeaker->size()==$ss+1?$s.=$speaker->$phone_name:$s.=$speaker->$phone_name.', ';
		}
		$profil->setVariable('speaker', $s);
		
		if($kkk==0){
			$profil->showBlock('first_p');
			$profil->hideBlock('second_p');
		}else{
			$profil->showBlock('second_p');
			$profil->hideBlock('first_p');
		}
		$profil->parseList('list_phone');
	}
	/*$profil->setVariable('ajax_upload_file', REP_AJAX .'profil/profil_photo_upload.php');
	$photo = modelUser::getProfilPhoto($user->id_user);
	if($photo->nom_serveur){
		$fichier = modelUploadFile::getObject($photo->id_fichier);
		$format = modelUploadFormat::getObject(FORMAT_100_80);
		$format_zoom = modelUploadFormat::getObject(FORMAT_PREVIEW_PHOTO);
		$url_image = '/'.$fichier->url_repertoire.$format->url_format.$fichier->nom_serveur;
		if(file_exists($photo->url_repertoire.$format->url_format.$photo->nom_serveur) and isImageFileName($photo->nom_serveur)){			
			if(file_exists($photo->url_repertoire.$format_zoom->url_format.$photo->nom_serveur) and isImageFileName($photo->nom_serveur)){				
				$url_image_zoom = '/'.$fichier->url_repertoire.$format_zoom->url_format.$fichier->nom_serveur;
			}else{
				$url_image_zoom = REP_DESIGN.'noimage.jpg';	
			}
		}
	}else{
		$url_image = REP_DESIGN.'nopic.gif';
	}
		$profil->setVariable('url_profil_photo', $url_image);
		$profil->setVariable('url_profil_photo_zoom', $url_image_zoom);	
		$profil->setVariable('title', $fichier->nom_initial);*/
}
	//$profil->setVariable('rep_id_avatar', ID_REP_AVATAR);

$profil->stop();
$page->setBody($profil);
$page->display();

?>